package net.asrsmid.config;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

/**
 * public class AsRsAuthenticator
 * <br>
 * extends {@link Authenticator}
 * <br><br>
 * An AsRsAuthenticator is used for the authentication required by Analysis Services or Reporting Services.
 * <br>
 * @author Chen WANG
 * @since  11 May 2011
 */
public class AsRsAuthenticator extends Authenticator {
	private AsRsAuthMode authMode;
	private String username;
	private String password;

	/**
	 * Constructs an AsRsAuthenticator with the given authMode.
	 * <br>
	 * The 'username' field and 'password' field will be ignored and set to null.
	 * @param authMode - The authentication mode used is Analysis Services or Reporting Services.
	 */
	public AsRsAuthenticator(AsRsAuthMode authMode) {
		this.authMode = authMode;
		this.username = null;
		this.password = null;
	}
	
	/**
	 * Constructs an AsRsAuthenticator with the given authMode, username and password.
	 * <br>
	 * @param authMode - The authentication mode used is Analysis Services or Reporting Services.
	 * @param username - The username required by Analysis Services or Reporting Services.
	 * @param password - The password required by Analysis Services or Reporting Services.
	 */
	public AsRsAuthenticator(AsRsAuthMode authMode, String username, String password) {
		this.authMode = authMode;
		this.username = username;
		this.password = password;
	}

	/**
	 * Called when password authorization is needed.
	 * @return a PasswordAuthentication object which contains username and password information.
	 */
	public PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(username, password.toCharArray());
	}

	/**
	 * Returns the authentication mode used in Analysis Services or in Reporting Services
	 * @return the authMode
	 */
	public AsRsAuthMode getAuthMode() {
		return authMode;
	}

	/**
	 * Returns the username mode used in Analysis Services or in Reporting Services, 
	 * could be null if the field is not initialized by user 
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * Returns the password mode used in Analysis Services or in Reporting Services, 
	 * could be null if the field is not initialized by user
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		sb.append("Authentication mode = ").append(authMode.name()).append(", Username = ").append(username);
		sb.append(", Password = ").append(password);
		
		return sb.toString();
	}
}